function [models_struct,raw_data,is_nan]=create_models_from_images(varargin)

% This function is one of the main functions in this system - it's building the trained data base.
% Inside, this function is a wrapper for the create_models_struct function - here we take a picture,
% calculating all it's features, getting it's ground truth and feeding it to the  create_models_struct.
% At the end we have a model's struct, that is made from an output from a classifier's run with 
% trained data. 

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% varagin - a list of data cells obtained from  the image GUI dechiper. each data should obtain 
% a 1X1 struct called saved_data, with 3 field inside - 
%                  saved_data{1,1} - labels' names
%                  saved_data{2,1} - ground_truth
%                  saved_data{3,1} - original image
% by default, this output is being made by the image GUI dechiper.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  models_struct - struct of models that are the output from a classifier.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% raw_data - a matrix of all the regions' features, in size of num_of_featuresXnum_of_regions for all
% then input matrices together.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% is_nan - 1 if model is not valid (NaN), 0 otherwise.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Global Inputs:
% model_p.norm_flag_seg
% model_p.norm_flag_reg
% model_p.hypo_option
% model_p.hypo_amount
% model_p.reg_per_hypo
% model_p.class_func
% model_p.if_cross_validation
% model_p.cross_validation_precent
% model_p.cross_validation_iteration
% model_p.bw_factor
% model_p.med_filter

run parameters 
global model_p
norm_flag_seg=model_p.norm_flag_seg;
norm_flag_reg=model_p.norm_flag_reg;
hypo_option=model_p.hypo_option;
hypo_amount=model_p.hypo_amount;
reg_per_hypo=model_p.reg_per_hypo;
class_func=model_p.class_func;
if_cross_validation=model_p.if_cross_validation;
cross_validation_precent=model_p.cross_validation_precent;
cross_validation_iteration=model_p.cross_validation_iteration;
 bw_factor=model_p.bw_factor;
 med_filter=model_p.med_filter;
 
input=[];
list_of_regions=[];
for i=1:nargin
    % extracting data from the dechiper's data
    fprintf('please wait - extracing picture number %d out of %d \n',i,nargin);
    data = open(varargin{i});
   image = data.saved_data{3,1};
    ground_truth = data.saved_data{2,1};
    
    % getting region's features
    res_struct=get_struct(image,norm_flag_seg,norm_flag_reg,hypo_option, hypo_amount, reg_per_hypo,bw_factor,med_filter);
    new_input=[res_struct.reg_features_cell{1,1}{1,1}.reg_loc_mat;res_struct.reg_features_cell{1,1}{1,1}.reg_text_mat;res_struct.reg_features_cell{1,1}{1,1}.reg_color_mat;res_struct.reg_features_cell{1,1}{1,1}.reg_geom_mat;res_struct.reg_features_cell{1,1}{1,1}.reg_shape_mat];
    [new_list]=which_region_from_ground_truth(ground_truth, res_struct.reg_indices{1,1});
    
    % adding the regions to the list till now
    input=[input new_input];
    list_of_regions=[list_of_regions new_list];
end

%running model building based on all the images together
raw_data.input=input;
raw_data.list_of_regions=list_of_regions;
[models_struct,is_nan] = create_models_struct( input,list_of_regions);

%running cross validation, if needed, for FLD classifier
if (strcmp(if_cross_validation,'true') & strcmp(class_func,'FLD') & is_nan~=1)
    list0=find(list_of_regions==0);
    input(:,list0)=[];
    list_of_regions(list0)=[];
    minor_size=length(list_of_regions);
    for i=1:cross_validation_iteration
        minor_size_list(i)=round(cross_validation_precent*i*minor_size);
    end
    models_struct=cross_validation (input,list_of_regions, minor_size_list);
end
